Page({
  data: {
    idc: "clear",
    idadd: "+",
    id9: "9",
    id8: "8",
    id7: "7",
    idj: "-",
    id6: "6",
    id5: "5",
    id4: "4",
    idx: "*",
    id3: "3",
    id2: "2",
    id1: "1",
    iddiv: "/",
    id0: "0",
    ide: "=",
    screenData: "0",
    operaSymbo: { "+": "+", "-": "-", "*": "*", "÷": "/" },
    lastIsOperaSymbo: false,
    iconType: 'waiting_circle',
    iconColor: 'white',
  },
  clickBtn(event) {
    //获取触发点击事件的标签的id
    var id = event.target.id
    if (id == this.data.idc) {//清屏
      this.setData({
        'screenData': '0'
      });
    } else if (id == this.data.ide) {//等于=
      var data = this.data.screenData;
      if (data == "0") {
        return;
      }
      var lastWord = data.charAt(data.length - 1);
      if (isNaN(lastWord)) {
        return;
      }
      var expression = this.data.screenData;
      //获取rpn库运算结果
      var result = this.calCommonExp(expression);
      this.setData({
        'screenData': result + ""
      });

    } else {//运算符和数字的问题
      if (this.data.operaSymbo[id]) { //如果是符号+-*/
        if (this.data.lastIsOperaSymbo) {
          //如果是最后一位是符号，就不能在收入符号
          return;
        }
      }
      var sd = this.data.screenData;
      var data;
      if (sd == 0) {
        data = id;
      } else {
        data = sd + id;
      }
      this.setData({
        'screenData': data
      });

      ///置一下最后一位是否为运算符的标志位
      if (this.data.operaSymbo[id]) {
        this.setData({ "lastIsOperaSymbo": true });
      } else {
        this.setData({ "lastIsOperaSymbo": false });
      }
    }
  },
  isOperator(value) {
    var operatorString = '+-*/()×÷';
    return operatorString.indexOf(value) > -1;
  }, getPriority(value) {
    if (value == '-' || value == '+') {
      return 1;
    } else if (value == '*' || value == '/' || value == '×' || value == '÷') {
      return 2;
    } else {
      return 0;
    }
  }, priority(v1, v2) {
    return this.getPriority(v1) <= this.getPriority(v2);
  }, outputRpn(exp) {
    var inputStack = [];
    var outputStack = [];
    var outputQueue = [];
    var firstIsOperator = false;
    exp.replace(/\s/g, '');
    if (this.isOperator(exp[0])) {
      exp = exp.substring(1);
      firstIsOperator = true;
    }
    for (var i = 0, max = exp.length; i < max; i++) {
      if (!this.isOperator(exp[i]) && !this.isOperator(exp[i - 1]) && (i != 0)) {
        inputStack[inputStack.length - 1] = inputStack[inputStack.length - 1] + exp[i] + '';
      } else {
        inputStack.push(exp[i]);
      }
    }
    if (firstIsOperator) {
      inputStack[0] = -inputStack[0]
    }
    while (inputStack.length > 0) {
      var cur = inputStack.shift();
      if (this.isOperator(cur)) {
        if (cur == '(') {
          outputStack.push(cur);
        } else if (cur == ')') {
          var po = outputStack.pop();
          while (po != '(' && outputStack.length > 0) {
            outputQueue.push(po);
            po = outputStack.pop();
          }
        } else {
          while (this.priority(cur, outputStack[outputStack.length - 1]) && outputStack.length > 0) {
            outputQueue.push(outputStack.pop());
          }
          outputStack.push(cur)
        }
      } else {
        outputQueue.push(Number(cur));
      }
    }
    if (outputStack.length > 0) {
      while (outputStack.length > 0) {
        outputQueue.push(outputStack.pop());
      }
    }
    return outputQueue;
  }, calRpnExp(rpnArr) {
    var stack = [];
    for (var i = 0, max = rpnArr.length; i < max; i++) {
      if (!this.isOperator(rpnArr[i])) {
        stack.push(rpnArr[i]);
      } else {
        var num1 = stack.pop();
        var num2 = stack.pop();
        if (rpnArr[i] == '-') {
          var num = num2 - num1;
        } else if (rpnArr[i] == '+') {
          var num = num2 + num1;
        } else if (rpnArr[i] == '*' || rpnArr[i] == '×') {
          var num = num2 * num1;
        } else if (rpnArr[i] == '/' || rpnArr[i] == '÷') {
          var num = num2 / num1;
        }
        stack.push(num);
      }
    }
    return stack[0];
  }, calCommonExp(exp) {
    var rpnArr = this.outputRpn(exp);
    return this.calRpnExp(rpnArr)
  }
})